package jmine.tec.di.scope;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.ListableBeanFactory;

/**
 * @author takeshi.fukushima
 */
public class BeanFactoryTypedScope extends AbstractTypedScope implements TypedScope {

    private final ListableBeanFactory beanFactory;

    /**
     * @param beanFactory o {@link ListableBeanFactory}
     */
    public BeanFactoryTypedScope(ListableBeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Map<String, Object> getLocalProperties() {
        return new HashMap<String, Object>();
    }

    /**
     * {@inheritDoc}
     */
    public void put(String key, Object value) {
        throw new IllegalStateException("Cannot insert on this scope");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean localContainsKey(String key) {
        return this.beanFactory.containsBean(key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Object localGet(String key, Class<?> type) {
        return this.beanFactory.getBean(key, type);
    }

}
